//除法函数，用来得到精确的除法结果   
//说明：javascript的除法结果会有误差，在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。   
//调用：accDiv(arg1,arg2)   
//返回值：arg1除以arg2的精确结果  

function accDiv(arg1, arg2) {
	var t1 = 0, t2 = 0, r1, r2;
	try {
		t1 = arg1.toString().split(".")[1].length
	} catch (e) {
	}
	try {
		t2 = arg2.toString().split(".")[1].length
	} catch (e) {
	}
	with (Math) {
		r1 = Number(arg1.toString().replace(".", ""))
		r2 = Number(arg2.toString().replace(".", ""))
		return ((r1 / r2) * pow(10, t2 - t1)).toFixed(3);
	}
}
// 给Number类型增加一个div方法，调用起来更加方便。
Number.prototype.div = function(arg) {
	return accDiv(this, arg);
}

// 乘法函数，用来得到精确的乘法结果
// 说明：javascript的乘法结果会有误差，在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
// 调用：accMul(arg1,arg2)
// 返回值：arg1乘以arg2的精确结果

function accMul(arg1, arg2) {
	var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length
	} catch (e) {
	}
	try {
		m += s2.split(".")[1].length
	} catch (e) {
	}
	return (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))
			/ Math.pow(10, m)).toFixed(3);
}
// 给Number类型增加一个mul方法，调用起来更加方便。
Number.prototype.mul = function(arg) {
	return accMul(arg, this);
}

// 加法函数，用来得到精确的加法结果
// 说明：javascript的加法结果会有误差，在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
// 调用：accAdd(arg1,arg2)
// 返回值：arg1加上arg2的精确结果

function accAdd(arg1, arg2) {
	var r1, r2, m;
	try {
		r1 = arg1.toString().split(".")[1].length
	} catch (e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split(".")[1].length
	} catch (e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2))
	return (arg1 * m + arg2 * m) / m
}
// 给Number类型增加一个add方法，调用起来更加方便。
Number.prototype.add = function(arg) {
	return accAdd(arg, this);
}

// 在你要用的地方包含这些函数，然后调用它来计算就可以了。
// 比如你要计算：7*0.8 ，则改成 (7).mul(8)
// 其它运算类似，就可以得到比较精确的结果。

// 减法函数
function accSubtr(arg1, arg2) {
	var r1, r2, m, n;
	try {
		r1 = arg1.toString().split(".")[1].length
	} catch (e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split(".")[1].length
	} catch (e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2));
	// last modify by deeka
	// 动态控制精度长度
	n = (r1 >= r2) ? r1 : r2;
	return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

// 给Number类型增加一个add方法，调用起来更加方便。
Number.prototype.subtract = function(arg) {
	return Subtr(arg, this);
}

/**
 * Formats the number according to the ‘format’ string; adherses to the american
 * number standard where a comma is inserted after every 3 digits. note: there
 * should be only 1 contiguous number in the format, where a number consists of
 * digits, period, and commas any other characters can be wrapped around this
 * number, including ‘$’, ‘%’, or text examples (123456.789): ‘0′ - (123456)
 * show only digits, no precision ‘0.00′ - (123456.78) show only digits, 2
 * precision ‘0.0000′ - (123456.7890) show only digits, 4 precision ‘0,000′ -
 * (123,456) show comma and digits, no precision ‘0,000.00′ - (123,456.78) show
 * comma and digits, 2 precision ‘0,0.00′ - (123,456.78) shortcut method, show
 * comma and digits, 2 precision
 * 
 * @method format
 * @param format
 *            {string} the way you would like to format this text
 * @return {string} the formatted number
 * @public
 */
function format(str, format) {
	alert(str);

	var hasComma = -1 < format.indexOf(',');
	var psplit = format.split('.');
	
	var that = str.toString();
	
	// compute precision
	if (1 < psplit.length) {
		// fix number precision
		that = (that * 1).toFixed(psplit[1].length);
	}
	// error: too many periods
	else if (2 < psplit.length) {
		throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
	}
	// remove precision
	else {
		that = that.toFixed(0);
	}

	// get the string now that precision is correct
	var fnum = that.toString();

	// format has comma, then compute commas
	if (hasComma) {
		// remove precision for computation
		psplit = fnum.split('.');

		var cnum = psplit[0], parr = [], j = cnum.length, m = Math.floor(j / 3), n = cnum.length % 3 || 3;
		for ( var i = 0; i < j; i += n) {
			if (i != 0) {
				n = 3;
			}
			parr[parr.length] = cnum.substr(i, n);
			m -= 1;
		}

		// put chunks back together, separated by comma
		fnum = parr.join(',');

		// add the precision back in
		if (psplit[1]) {
			fnum += '.' + psplit[1];
		}
	}

	// replace the number portion of the format with fnum
	return format.replace(/[d,?.?]+/, fnum);
}

function unFormart(format)
{
	if(format == null || format == '')
	{
		return format;
	}
	
	return format.toString().replace(/\,/g, "");
}
